﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity.Car_Rental;
using Entity.CommonEnums;

namespace Data.Car_Rental
{
    public class NewsRepository : Car_RentalRepositoryBase, INewsRepository
    {
        #region IRepository<News> 成员

        public void Update(News entity)
        {
            base.UpdateEntity(entity);
        }

        public void Update(IList<News> list)
        {
            base.UpdateEntity(list.Cast<Entity.EntityBase>().ToList());

        }

        public void Insert(News entity)
        {
            base.InsertEntity(entity);
        }

        public void Insert(IList<News> list)
        {
            base.InsertEntity(list.Cast<Entity.EntityBase>().ToList());
        }

        public News InsertGetIDENTITY(News entity)
        {
            base.InsertEntity(entity);
            return this.Find(entity.PrimaryKey);
        }

        public void Delete(News entity)
        {
            base.DeleteEntity(entity);
        }

        public void Delete(IList<News> list)
        {
            base.DeleteEntity(list.Cast<Entity.EntityBase>().ToList());
        }

        public IQueryable<News> GetModel()
        {
            return base.GetEntities<News>().Select(i => new News_Ext
            {
                CreateDate = i.CreateDate,
                DisplayDate = i.DisplayDate,
                InfoSummary = i.InfoSummary,
                Info = i.Info,
                NewsID = i.NewsID,
                Status = i.Status,
                Title = i.Title,
                UpdateDate = i.UpdateDate,
            });
        }

        public News Find(params object[] keyValues)
        {
            return base.Find<News>(keyValues);
        }

        #endregion

        #region INewsRepository 成员

        public IQueryable<News> GetModelForSite()
        {
            return this.GetModelDetail().OrderByDescending(i => i.NewsID).Where(i => i.Status == (int)Status.Normal);
        }

        public IQueryable<News> GetModelForBackground()
        {
            return this.GetModelDetail().OrderByDescending(i => i.NewsID);
        }

        public IQueryable<News> GetModelDetail()
        {
            var linq = from i in this.GetModel()
                       // join j in new HelperCenterCategoryRepository().GetModel() on i.HelpCategoryID equals j.HelpCategoryID
                       select new News_Ext
                        {
                            CreateDate = i.CreateDate,
                            DisplayDate = i.DisplayDate,
                            InfoSummary = i.InfoSummary,
                            Info = i.Info,
                            NewsID = i.NewsID,
                            Status = i.Status,
                            Title = i.Title,
                            UpdateDate = i.UpdateDate,
                            //  HelperCenterCategory_Extend = j,
                        };
            return linq;
        }

        public Entity.PagedList<News> GetNews(Entity.VPredication vp, Entity.PagingParam pp)
        {
            var linq = this.GetModelDetail();
            return new Entity.PagedList<News>(linq, pp.PageIndex, pp.PageSize);
        }
        #endregion
    }
}
